
//
// 121226 - AH - Created.
//

package org.japril.swing.jutil.logging;

import java.util.List;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.japril.jutil.logging.ControlLogSink.ClassLogInfo;
import org.japril.jutil.logging.ControlLogSink.MethodLogInfo;

/**
 * <p>
 *   {@link TableModel} that adapts the
 *   {@link MethodLogInfo} object to a
 *   {@link ClassLogInfo} for a
 *   {@link JTable} view.
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class ControlLogSinkClassInfoTableModel extends AbstractTableModel
{
  /**
   * <p>
   *   The empty model shared.
   * </p>
   */
  public static final ControlLogSinkClassInfoTableModel EMPTY_MODEL=
    new ControlLogSinkClassInfoTableModel();
  
  private final List<MethodLogInfo> m_info;
  
  // Creates the shared empty model.
  private ControlLogSinkClassInfoTableModel(){ m_info=null; }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param cli {@link ClassLogInfo} to show info for.
   */
  public ControlLogSinkClassInfoTableModel(ClassLogInfo cli)
  {
    if (cli==null)
    {
      throw new NullPointerException("cli");
    }
    
    m_info=cli.getMethodLogInfos();
  }
  
  // Specified in TableModel.
  public int getColumnCount(){ return 2; }

  // Specified in TableModel.
  public int getRowCount(){ return m_info==null ? 0 : m_info.size(); }

  // Specified in TableModel.
  public Object getValueAt(int row, int col)
  {
    if (m_info==null || row<0 || row>=m_info.size())
    {
      throw new IndexOutOfBoundsException("row: "+row);
    }
    
    MethodLogInfo mli=m_info.get(row);
    switch (col)
    {
      case 0 : return mli.getMethod();
      case 1 : return mli.log();
      default : throw new IndexOutOfBoundsException("col: "+col);
    }
  }

  // Specified in TableModel.
  public Class<?> getColumnClass(int col)
  {
    switch (col)
    {
      case 0 : return String.class;
      case 1 : return Boolean.class;
      default : throw new IndexOutOfBoundsException(Integer.toString(col));
    }
  }

  // Specified in TableModel.
  public String getColumnName(int col) 
  {
    switch (col)
    {
      case 0 : return "Method Name";
      case 1 : return "Logs";
      default : throw new IndexOutOfBoundsException(Integer.toString(col));
    }
  }

  // Specified in TableModel.
  public boolean isCellEditable(int row, int col)
  {
    if (m_info==null || row<0 || row>=m_info.size())
    {
      throw new IndexOutOfBoundsException("row: "+row);
    }
    
    switch (col)
    {
      case 0 : return false;
      case 1 : return true;
      default : throw new IndexOutOfBoundsException(Integer.toString(col));
    }
  }

  // Specified in TableModel.
  public void setValueAt(Object val, int row, int col)
  {
    if (m_info==null || row<0 || row>=m_info.size())
    {
      throw new IndexOutOfBoundsException("row: "+row);
    }
    
    switch (col)
    {
      case 0 :
      case 2 : throw new IllegalArgumentException("col: "+col+" not editable");
      case 1 :
      {
        MethodLogInfo mli=m_info.get(row);
        mli.log((Boolean)val);
        fireTableCellUpdated(row, col);
        return;  
      }
      
      default : throw new IndexOutOfBoundsException(Integer.toString(col));
    }
  }
  
}
